package com.example.shopping.fpage;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.google.gson.Gson;
import com.example.shopping.R;
import com.example.shopping.goods.TypeBean;
import com.example.shopping.utils.BaseFragment;
import com.example.shopping.utils.URLUtils;

import java.util.ArrayList;
import java.util.List;
/**
 * 首页碎片
 */
public class FirstPageFragment extends BaseFragment {
    ViewPager typeVp,lbVp;
    LinearLayout typeLayout,lpLayout;

    List<ImageView>lbviewList;
    List<ImageView>lbPointList;  //轮播下面小点点的集合

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1) {
                int currentItem = lbVp.getCurrentItem(); //获取当前播放的位置
                if (currentItem == lbviewList.size()-1) {
                    lbVp.setCurrentItem(0);  //播放第一张
                }else {
                    lbVp.setCurrentItem(currentItem+1);
                }
                handler.sendEmptyMessageDelayed(1,3000);
            }
        }
    };
    int totalPage;   //表示总的页数
    int pageSize = 8;  //每页的最大数量
    List<GridView>typeviewList;  //分类的viewpager的数据源
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_first_page, container, false);
        initView(view);
        typeviewList = new ArrayList<>();
        setLBData();   //设置轮播图显示数据
        setLBListener();  //设置轮播监听事件
        handler.sendEmptyMessageDelayed(1,3000);
        connectNetwork(URLUtils.allCategory);   //联网
        return view;
    }
    // 表示联网成功时，会调用的方法
    @Override
    public void onSuccess(String result) {
        TypeBean bean = new Gson().fromJson(result, TypeBean.class);
        List<TypeBean.DataBean> list = bean.getData();
        totalPage = (int)Math.ceil(list.size()*1.0/pageSize);
        for (int i = 0; i < totalPage; i++) {
            GridView gridView = new GridView(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.setMargins(10,10,10,10);
            gridView.setLayoutParams(params);
            gridView.setNumColumns(pageSize/2);

            //设置适配器
            FPTypeAdapter adapter = new FPTypeAdapter(getContext(), list, i, pageSize);
            gridView.setAdapter(adapter);
            typeviewList.add(gridView);
        }
        //ViwePager适配器
        FPCatagoryAdapter catagoryAdapter = new FPCatagoryAdapter(typeviewList);
        typeVp.setAdapter(catagoryAdapter);
    }

    /* 设置轮播图的监听事件*/
    private void setLBListener() {
        lbVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < lbPointList.size(); i++) {
                    lbPointList.get(i).setImageResource(R.mipmap.page__normal_indicator);
                }
                lbPointList.get(position).setImageResource(R.mipmap.page__selected_indicator);
            }
        });
    }

    private void setLBData() {
        lbviewList = new ArrayList<>();
        lbPointList = new ArrayList<>(); //小点点初始化
        int[]resId = {R.mipmap.ad1,R.mipmap.ad2};
        for (int i = 0; i < resId.length; i++) {
            ImageView iv = new ImageView(getContext());
            iv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.setImageResource(resId[i]);
            lbviewList.add(iv);
            //小点点初始化
            ImageView piv = new ImageView(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.setMargins(8,0,8,0);
            piv.setLayoutParams(params);
            piv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            piv.setImageResource(R.mipmap.page__normal_indicator);
            lpLayout.addView(piv);
            lbPointList.add(piv);
        }
        lbPointList.get(0).setImageResource(R.mipmap.page__selected_indicator);
        LunboAdapter lunboAdapter = new LunboAdapter(lbviewList);
        lbVp.setAdapter(lunboAdapter);
    }
    /* 初始化控件 */
    private void initView(View view) {
        typeVp = view.findViewById(R.id.frag_fp_vp_type);
        lbVp = view.findViewById(R.id.frag_fp_vp_lb);
        typeLayout = view.findViewById(R.id.frag_fp_layout_p1);
        lpLayout = view.findViewById(R.id.frag_fp_layout_p2);
    }
}